import os
import sys
import logging
from datetime import datetime, timedelta


class MyLog(object):
    def __init__(self, disable_logging=False):
        # 输出日志路径
        PATH = os.path.dirname(os.path.abspath(__file__))+'\\logs\\'
        # 设置日志格式和时间格式
        FMT = '%(asctime)s %(levelname)s: %(message)s'
        DATEFMT = '%Y-%m-%d %H:%M:%S'
        self.logger = logging.getLogger()
        self.formatter = logging.Formatter(fmt=FMT, datefmt=DATEFMT)
        self.log_filename = '{0}{1}.log'.format(PATH, datetime.now().strftime("%Y-%m-%d"))
        self.log_filename = os.path.join(PATH, self.log_filename)  # 使用 os.path.join 确保跨平台兼容性

        if disable_logging:
            self.logger.setLevel(logging.CRITICAL)  # 禁用日志记录
        else:
            self.logger.addHandler(self.get_file_handler(self.log_filename))
            self.logger.addHandler(self.get_console_handler())
            # 设置日志的默认级别
            logging.getLogger('apscheduler.executors.default').propagate = False
            self.logger.setLevel(logging.INFO)

    # 输出到文件handler的函数定义
    def get_file_handler(self, filename):
        filehandler = logging.FileHandler(filename, encoding="utf-8")
        filehandler.setFormatter(self.formatter)
        return filehandler

    # 输出到控制台handler的函数定义
    def get_console_handler(self):
        console_handler = logging.StreamHandler(sys.stdout)
        console_handler.setFormatter(self.formatter)
        return console_handler


class Encryption:
    def __init__(self):
        # 初始化加密算法
        self.my_logg = MyLog().logger

    def time_cha(self):
        # 获取当前时间
        current_time = datetime.now()
        # 判断当前时间是否在早上八点到晚上八点之间
        if 8 <= current_time.hour < 20:
            # 如果在早上八点到晚上八点之间，则输出早上八点到晚上八点的时间
            start_time = current_time.replace(hour=8, minute=0, second=0, microsecond=0)
            end_time = current_time.replace(hour=20, minute=0, second=0, microsecond=0)
            self.my_logg.info("早上八点到晚上八点的时间：%s",
                              start_time.strftime("%Y-%m-%d %H:%M:%S") + " - " + end_time.strftime("%Y-%m-%d %H:%M:%S"))
        elif current_time.hour < 8:
            # 如果当前时间在晚上八点到次日早上八点之间，则输出昨日20点到8点的时间
            start_time = current_time - timedelta(days=1)
            start_time = start_time.replace(hour=20, minute=0, second=0, microsecond=0)
            end_time = current_time.replace(hour=8, minute=0, second=0, microsecond=0)
            self.my_logg.info("昨日20点到8点的时间：%s",
                              start_time.strftime("%Y-%m-%d %H:%M:%S") + " - " + end_time.strftime("%Y-%m-%d %H:%M:%S"))
        else:
            # 如果当前时间在早上八点到晚上八点之间，则输出今日20点到8点的时间
            start_time = current_time.replace(hour=20, minute=0, second=0, microsecond=0)
            end_time = current_time.replace(hour=8, minute=0, second=0, microsecond=0) + timedelta(days=1)
            self.my_logg.info("今日20点到明天8点的时间：%s",
                              start_time.strftime("%Y-%m-%d %H:%M:%S") + " - " + end_time.strftime("%Y-%m-%d %H:%M:%S"))
        self.startTime = start_time.strftime("%Y-%m-%d %H:%M:%S")
        self.end_time = end_time.strftime("%Y-%m-%d %H:%M:%S")

        return self
